package com.example.wx.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.example.wx.common.enums.WxPublicStatusEnum;
import com.example.wx.dao.entity.WxPublicMainDO;
import com.example.wx.dao.entity.WxPublicMainExtendDO;
import com.example.wx.dao.entity.WxPublicMessageTemplateDO;
import com.example.wx.dao.manager.WxPublicMessageTemplateManager;
import com.example.wx.dao.mapper.WxPublicMainMapper;
import com.example.wx.dao.mapper.WxPublicMessageTemplateMapper;
import com.example.wx.modle.accessToken.AccessToken;
import com.example.wx.modle.message.WxMessageTemplateDTO;
import com.example.wx.modle.req.WxPublicMainAddReqDTO;
import com.example.wx.modle.req.WxPublicMainStatusReqDTO;
import com.example.wx.modle.resp.WxPublicMainRespDTO;
import com.example.wx.other.BizException;
import com.example.wx.other.ResponseDTO;
import com.example.wx.service.WxPublicMainService;
import com.example.wx.service.convertor.WxPublicMainConvertor;
import com.example.wx.service.wx.WxService;
import com.example.wx.util.MyBeanUtils;
import com.vivo.it.vwkc.inventory.common.entity.request.CommonRequestPage;
import com.vivo.it.vwkc.inventory.common.entity.response.CommonResponsePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 微信公众号主表
 *
 * @author ZhangBin
 * @date 2025-04-08 09:54:36
 * @Description:
 */
@Service
public class WxPublicMainServiceImpl implements WxPublicMainService {
    @Autowired
    private WxPublicMainMapper wxPublicMainMapper;
    @Autowired
    private WxPublicMessageTemplateMapper wxPublicMessageTemplateMapper;
    @Autowired
    private WxPublicMessageTemplateManager wxPublicMessageTemplateManager;



    @Override
    public ResponseDTO<CommonResponsePage<?>> queryPage(CommonRequestPage commonRequestPage) {
        return null;
    }

    @Override
    public WxPublicMainRespDTO queryDetail(Long id) {
        WxPublicMainDO wxPublicMainDO = wxPublicMainMapper.queryObject(id);
        if(Objects.isNull(wxPublicMainDO)){
            throw new BizException("获取公众号信息为空");
        }
        return MyBeanUtils.copy(wxPublicMainDO,WxPublicMainRespDTO.class);
    }

    @Override
    public void add(WxPublicMainAddReqDTO wxPublicMainAddReqDTO) {
        WxPublicMainDO wxPublicMainDO = wxPublicMainMapper.queryObjectByAppId(wxPublicMainAddReqDTO.getWxPublicAppId());
        if(!Objects.isNull(wxPublicMainDO)){
            throw new BizException("公众号已存在");
        }
        AccessToken accessToken;
        try{
            accessToken = WxService.getAccessToken(wxPublicMainAddReqDTO.getWxPublicAppId(), wxPublicMainAddReqDTO.getWxPublicAppSecret());
        }catch (Exception e){
            throw new BizException("添加失败，请检查appId和appSecret是否正确，是否添加ip白名单到微信公众号平台");
        }
        //组装住表数据
        WxPublicMainExtendDO wxPublicMainExtendDO = WxPublicMainConvertor.buildWxPublicMainDO(wxPublicMainAddReqDTO,accessToken);
        wxPublicMainMapper.save(wxPublicMainExtendDO);

        //拉取模板列表
        List<WxMessageTemplateDTO.TemplateList> templateList = WxService.getTemplateList(accessToken.getAccess_token());
        List<WxPublicMessageTemplateDO> wxPublicMessageTemplateDOList = WxPublicMainConvertor.buildWxPublicMessageTemplateDO(templateList,wxPublicMainExtendDO.getWxPublicAppId());
        wxPublicMessageTemplateManager.batchInsert(wxPublicMessageTemplateDOList);
    }

    @Override
    public void enable(WxPublicMainStatusReqDTO wxPublicMainStatusReqDTO) {
        List<WxPublicMainDO> wxPublicMainDOS = queryWxPublicMainByIds(wxPublicMainStatusReqDTO.getIds());
        List<WxPublicMainExtendDO> wxPublicMainExtendDOS = WxPublicMainConvertor.buWxPublicMainExtendDOList(wxPublicMainDOS, WxPublicStatusEnum.ENABLE);
        wxPublicMainMapper.batchUpdate(wxPublicMainExtendDOS);
    }

    @Override
    public void disable(WxPublicMainStatusReqDTO wxPublicMainStatusReqDTO) {
        List<WxPublicMainDO> wxPublicMainDOS = queryWxPublicMainByIds(wxPublicMainStatusReqDTO.getIds());
        List<WxPublicMainExtendDO> wxPublicMainExtendDOS = WxPublicMainConvertor.buWxPublicMainExtendDOList(wxPublicMainDOS, WxPublicStatusEnum.DISABLE);
        wxPublicMainMapper.batchUpdate(wxPublicMainExtendDOS);
    }

    @Override
    public void delete(WxPublicMainStatusReqDTO wxPublicMainStatusReqDTO) {
        List<WxPublicMainDO> wxPublicMainDOS = queryWxPublicMainByIds(wxPublicMainStatusReqDTO.getIds());
        List<WxPublicMainExtendDO> wxPublicMainExtendDOS = WxPublicMainConvertor.buWxPublicMainExtendDOList(wxPublicMainDOS);
        wxPublicMainMapper.batchUpdate(wxPublicMainExtendDOS);
    }

    private List<WxPublicMainDO> queryWxPublicMainByIds(List<Long> ids) {
        List<WxPublicMainDO> wxPublicMainDOS = wxPublicMainMapper.queryList(new WxPublicMainExtendDO().setIds(ids));
        if(CollectionUtil.isEmpty(wxPublicMainDOS)){
            throw new BizException("获取公众号信息为空");
        }
        return wxPublicMainDOS;
    }

}
